<template>
  <div class="cate">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-button type="primary" @click="addCateBtn">添加分类</el-button>
      </el-row>
      <!-- 表格 -->
      <tree-table
        ref="singleTable"
        :data="cateList"
        style="margin-top: 30px"
        highlight-current-row
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        :border="true"
      >
        <!-- 是否有效 -->
        <template slot="isOk" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === 'true'"
          ></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 排序 模板 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-if="scope.row.cat_level === 2"
            >三级</el-tag
          >
        </template>
        <!-- 操作 模板 -->
        <template slot="opt">
          <el-button class="el-icon-edit" type="primary">编辑</el-button>
          <el-button class="el-icon-delete" type="danger">删除</el-button>
        </template>
      </tree-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      style="margin-top: 30px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.currentSize"
      :page-sizes="[3, 6, 9, 11]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 添加分类  对话框 -->
    <el-dialog
      :visible.sync="addCateDialogVisible"
      width="30%"
      @close="addCateDialogClosed"
    >
      <el-form
        :model="addCateForm"
        ref="addCateFormRef"
        label-width="80px"
        :rules="addCateFormRules"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="username">
          <!-- options用来指定数据源 -->
          <el-cascader
            clearable
            expand-trigger="hover"
            :options="cascaderList"
            checkStrictly="true"
            :props="cascaderProps"
            v-model="selectedKeys"
            @change="parentCateChange"
          >
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [], // 分类列表
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有序',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      queryInfo: {
        type: 3,
        pagesize: 3,
        pagenum: 1
      },
      total: 0,
      cascaderList: [], // 添加分类的列表
      addCateDialogVisible: false, // 添加分类对话框
      addCateFormRef: [],
      // 添加分类表单数据
      addCateForm: {
        // 将要添加的分类名称
        cat_name: '',
        // 父级分类的id
        cat_pid: 0,
        // 分类的等级  默认一级
        cat_level: 0
      },
      // 添加分类表单的验证规则对象
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedKeys: [],
      options: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取商品分类列表数据
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryInfo
      })
      console.log(res)
      if (res.meta.status !== 200) {
        // 此处必须加return   不然程序会继续执行下去
        return this.$message.error('获取分类列表失败！')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监听页码变化
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newCurrentSize) {
      this.queryInfo.pagenum = newCurrentSize
      this.getCateList()
    },
    // 监听分类对话框的关闭事件
    addCateDialogClosed() {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },
    // 添加分类
    addCate() {
      // 预检验表单
      this.$refs.addCateFormRef.validate(async vaild => {
        if (!vaild) return false
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateForm
        )
        console.log(res)
        if (res.meta.status !== 201) {
          // 此处必须加return   不然程序会继续执行下去
          return this.$message.error('添加分类失败！')
        }
        // 添加用户成功后   刷新页面
        this.getCateList()
        // 隐藏添加分类的对话框
        this.addCateDialogVisible = false
      })
    },
    // 获取添加分类的列表数据
    async getCascaderList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      console.log(res)
      if (res.meta.status !== 200) {
        // 此处必须加return   不然程序会继续执行下去
        return this.$message.error('获取分类列表失败！')
      }
      this.cascaderList = res.data
    },
    addCateBtn() {
      this.addCateDialogVisible = true
      this.getCascaderList()
    },
    // 监听父级文本框的变化
    parentCateChange() {
      // 数组
      console.log(this.selectedKeys)
      if (this.selectedKeys.length > 0) {
        this.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        this.cat_level = this.selectedKeys.length
        // return false
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    }
  }
}
</script>

<style lang="less" scoped>
.cate {
  .el-card {
    margin-top: 30px;
  }
}
</style>
